package com.bitauto.i.commonUtils.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

public class AtomicInteger01 {
    AtomicInteger count = new AtomicInteger(0);
    static LongAdder longAdder = new LongAdder();

    void m(){
        count.incrementAndGet();
    }

    private static  void AtomicIntegerTestMethod(){
        AtomicInteger01 t = new AtomicInteger01();
        List<Thread> threads =new ArrayList<Thread>();
        for(int i=0;i<100;i++){
            threads.add(new Thread(t::m,"thread"+i));
        }
        threads.forEach((o)->o.start());

        threads.forEach((o)->{
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(t.count);
    }


    public static void main(String[] args) {
        List<Thread> threads =new ArrayList<Thread>();

        for(int i=0;i<100;i++){
            threads.add(new Thread(
                    ()->{
                        for(int j=0;j<10000;j++){
                            longAdder.increment();
                        }
                    }
            ));
        }

        threads.forEach((o)->o.start());

        threads.forEach((o)->{
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(longAdder.longValue());
    }



}
